package com.jerry.utils.zbx;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jerry.utils.str.JsonUtils;
import com.jerry.utils.str.Jutils;

public class ZBX {

	private static final Logger log = LoggerFactory.getLogger(ZBX.class);

	public static JSONObject sendPostRequest(ZBXReq params) {

		RestTemplate restTemplate = new RestTemplate();
		HttpHeaders headers = new HttpHeaders();

		// 设置类型
		MediaType type = MediaType.parseMediaType("application/json-rpc; charset=UTF-8");
		headers.setContentType(type);

		HttpEntity<String> formEntity = new HttpEntity<String>(JSONObject.toJSON(params).toString(), headers);

		// 发送数据方法
		ResponseEntity<String> forEntity = restTemplate.postForEntity(Jutils.getProperty("zbx.apiurl"), formEntity,
				String.class);

		// 得到返回的数据body
		String jsonData = forEntity.getBody();
		log.debug("json data is:" + jsonData);

		return JSONObject.parseObject(jsonData);

	}

	public static boolean createHost(String host, int port) {
		boolean ret = false;
		ZBXReq zReq = new ZBXReq();
		String authStr = ZBX.userLogin();
		zReq.setAuth(authStr);
		zReq.setMethod("host.create");
		JSONObject params = new JSONObject();
		params.put("host", host + "-" + port);
		JSONObject interObj = new JSONObject();
		interObj.put("type", Integer.parseInt(Jutils.getProperty("host.interftype")));
		interObj.put("main", 1);
		interObj.put("useip", 1);
		interObj.put("ip", host);
		interObj.put("dns", "");
		interObj.put("port", port);
		JSONArray interArray = new JSONArray();
		interArray.add(interObj);
		params.put("interfaces", interArray);

		JSONObject groupsObj = new JSONObject();
		groupsObj.put("groupid", Jutils.getProperty("host.groupid"));
		JSONArray groupArray = new JSONArray();
		groupArray.add(groupsObj);
		params.put("groups", groupArray);

		JSONObject tempObj = new JSONObject();
		tempObj.put("templateid", Jutils.getProperty("host.templateid"));
		JSONArray tempArray = new JSONArray();
		tempArray.add(tempObj);
		params.put("templates", tempArray);
		zReq.setParams(params);

		log.debug("params is: " + JSONObject.toJSON(zReq).toString());
		try {
			JSONObject retJson = ZBX.sendPostRequest(zReq);
			log.debug("return code is: " + retJson.getInteger("result.error.code"));
			if (retJson.getInteger("result.error.code") < 0) {

				ret = false;
			} else if (!retJson.getString("result.hostids[0]").isEmpty()) {
				ret = true;
			}
		} catch (Exception e) {
			// TODO: handle exception
		}

		return ret;

	}

	public static String userLogin() {
		String user = Jutils.getProperty("zbx.user");
		String passwd = Jutils.getProperty("zbx.passwd");
		ZBXReq zReq = new ZBXReq();
		JSONObject jo = new JSONObject();
		zReq.setMethod("user.login");
		jo.put("user", user);
		jo.put("password", passwd);
		zReq.setParams(jo);
		log.debug(JSONObject.toJSON(zReq).toString());
		JSONObject ret = ZBX.sendPostRequest(zReq);
		log.debug((String) ret.get("result"));
		return ret.get("result").toString();

	}

	public void getVersion() {
		ZBXReq zReq = new ZBXReq();
		JSONObject jo = new JSONObject();
		zReq.setMethod("host.get");
		zReq.setParams(jo);
		log.debug(JSONObject.toJSON(zReq).toString());
		JSONObject ret = ZBX.sendPostRequest(zReq);
		log.debug((String) ret.get("result"));
	}

	public static JSONObject getHost() {
		ZBXReq zReq = new ZBXReq();
		JSONObject jo = new JSONObject();
		zReq.setMethod("host.get");
		zReq.setAuth(ZBX.userLogin());
		zReq.setParams(jo);
		log.debug(JSONObject.toJSON(zReq).toString());
		JSONObject ret = ZBX.sendPostRequest(zReq);
		log.debug(ret.toJSONString());
		return ret;
	}

	public static List<String> getHostList(JSONObject host) {
		List<String> ret = new ArrayList<>();
		JSONArray hostArray = host.getJSONArray("result");
		for (int i = 0; i < hostArray.size(); i++) {
			String ipStr = "";
			if (Jutils.ipCheck(((JSONObject) host.getJSONArray("result").get(i)).getString("name"))) {
				ipStr = ((JSONObject) host.getJSONArray("result").get(i)).getString("name");
				ret.add(ipStr);
				log.debug("name is:" + ipStr);
			} else if (Jutils.ipCheck(((JSONObject) host.getJSONArray("result").get(i)).getString("host"))) {
				ipStr = ((JSONObject) host.getJSONArray("result").get(i)).getString("host");
				log.debug("host is:" + ipStr);
				ret.add(ipStr);
			}

		}
		return ret;
	}

	public static void main(String[] args) {

		JSONObject jObject = new JSONObject();
		try {
			jObject = JsonUtils.readJsonFromClassPath("test.json");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		log.debug("ip: " + ZBX.getHostList(jObject).get(100));
		

	}

}
